﻿using Mapster;
using MapsterMapper;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace Luka;

/// <summary>
/// 映射
/// </summary>
[NotScanned]
public static class MapsterSetup
{
    /// <summary>
    /// 注册中间件
    /// </summary>
    /// <returns></returns>
    public static WebApplicationBuilder AddMapster(this WebApplicationBuilder builder)
    {
        //获取继承 IRegister 的程序集
        var assembly = InternalApp.TypeList
            .Where(it => !it.Equals(typeof(IRegister)))
            .Where(it => typeof(IRegister).IsAssignableFrom(it))
            .Select(it => it.Assembly)
            .ToArray();
        //配置项
        var config = TypeAdapterConfig.GlobalSettings;
        config.Scan(assembly);
        config.Default.NameMatchingStrategy(NameMatchingStrategy.Flexible).PreserveReference(true);
        config.Default.NameMatchingStrategy(NameMatchingStrategy.IgnoreCase).PreserveReference(true);
        builder.Services.AddSingleton(config).AddScoped<IMapper, ServiceMapper>();
        return builder;
    }
}